#include "task.h"
#include "timer_loop.h"
#include "when_all.h"
#include <iostream>
#include <thread>

co_async::TimerLoop loop;

co_async::Task<int> hello1(int num) {
  std::cout << "hello1: " << num << std::endl;
  co_await co_async::sleep_for(loop, std::chrono::seconds(num));
  co_return num;
}

co_async::Task<int> hello2(int num) {
  std::cout << "hello2: " << num << std::endl;
  co_await co_async::sleep_for(loop, std::chrono::seconds(num));
  co_return num;
}
co_async::Task<int> hello() {
  std::cout << "begin hello()" << std::endl;

  auto res = co_await co_async::when_all(hello1(4), hello2(4));
  std::cout << std::get<0>(res) << std::get<1>(res) << std::endl;
  co_return std::get<0>(res) + std::get<1>(res);
}

int main() {
  auto t = hello();
  t.get_handle().resume();
  while (!t.get_handle().done()) {
    if (auto delay = loop.run()) {
      std::this_thread::sleep_for(delay.value());
    }
  }

  std::cout << "hello result:" << t.get_handle().promise().result()
            << std::endl;
}